/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.slice;

import com.lingeringdream.luciole.ResourceTable;
import com.lingeringdream.luciole.model.entity.User;
import com.lingeringdream.luciole.model.helper.GetEntityHelper;
import com.squareup.picasso.Picasso;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.text.SimpleDateFormat;

/**
 * 用户信息页面
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/5/4
 */
public class UserInfoSlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x11, UserInfoSlice.class.getSimpleName());
    private User user;
    private final boolean change = false;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_slice_user_info);
        long uid = intent.getLongParam("uid", -1L);
        if (uid != -1L) {
            user = GetEntityHelper.getUser(getContext(), uid);
        } else {
//            TODO 异常处理

        }
        if (user != null) {
            initAvatars();
            initInfo();
        } else {
//                TODO 提示用户不存在

        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        if (change) {
            upload();
        }
    }

    /**
     * 初始化头像
     */
    private void initAvatars() {
        Image avatar = findComponentById(ResourceTable.Id_userDetail_avatars);
        avatar.setRotation(100f);
        Picasso.get()
                .load(user.getAvatars())
                .error(ResourceTable.Media_avatars_default)
                .fit()
                .into(avatar);
        avatar.setClickedListener(component -> {
//            TODO 弹出对话框：查看大图   或者更换头像
        });
    }

    /**
     * 初始化用户信息
     */
    private void initInfo() {
        DirectionalLayout detailLayout = findComponentById(ResourceTable.Id_userDetail_details);
//        昵称
        Component item = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item, null, false);
        Text title = item.findComponentById(ResourceTable.Id_detail_item_title);
        title.setText("昵称");
        Text content = item.findComponentById(ResourceTable.Id_detail_item_content);
        content.setText(user.getUname());
        item.setClickedListener(component -> {
//            TODO 跳转对话框：修改昵称

        });
        detailLayout.addComponent(item);
//        性别
        item = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item, null, false);
        title = item.findComponentById(ResourceTable.Id_detail_item_title);
        title.setText("性别");
        content = item.findComponentById(ResourceTable.Id_detail_item_content);
        content.setText(user.getGender() ? "男" : "女");
        item.setClickedListener(component -> {
//            TODO 弹出对话框：更改性别
        });
        detailLayout.addComponent(item);
//        生日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        item = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item, null, false);
        title = item.findComponentById(ResourceTable.Id_detail_item_title);
        title.setText("生日");
        content = item.findComponentById(ResourceTable.Id_detail_item_content);
        content.setText(user.getBirth() == 0 ? "赶快设置你的生日吧~" : sdf.format(user.getBirth()));
        item.setClickedListener(component -> {
//            TODO 弹出对话框：设置生日
        });
        detailLayout.addComponent(item);
//        手机号
        item = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item, null, false);
        title = item.findComponentById(ResourceTable.Id_detail_item_title);
        title.setText("手机号");
        content = item.findComponentById(ResourceTable.Id_detail_item_content);
        content.setText(user.getTel().split("-")[1]);
        item.setClickedListener(component -> {
//            TODO 弹出对话框：修改手机号
        });
        detailLayout.addComponent(item);
//        个人简介
        item = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item_long, null, false);
        title = item.findComponentById(ResourceTable.Id_detail_item_title);
        title.setText("个人简介");
        content = item.findComponentById(ResourceTable.Id_detail_item_content);
        content.setText(user.getBrief());
        item.setClickedListener(component -> {
//            TODO 跳转页面：修改个人简介
        });
        detailLayout.addComponent(item);
    }

    /**
     * 上传修改
     */
    private void upload() {

    }
}
